package org.jeecg.common.util;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author lixingxing
 */
@Slf4j
@Component
public class Html2OfficeUtil {
    private static final ChineseFontProvider PROVIDER = new ChineseFontProvider();

    static class ChineseFontProvider extends XMLWorkerFontProvider {
        @Override
        public Font getFont(String fontname, String encoding, float size, int style) {
            try {
                BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
                return new Font(baseFont, size, style);
            } catch (DocumentException e) {
                log.error(e.getMessage(), e);
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
            return super.getFont(fontname, encoding, size, style);
        }
    }
}
